﻿#include "MacType.h"
#include <string.h>
#include <iostream>

MacType::MacType()
{
    memset(m_addr, 0x00, sizeof(m_addr));
}

MacType::MacType(std::initializer_list<unsigned char> data)
{
    int index = 0;
    for (auto iter = data.begin(); iter != data.end() && index < 6; ++iter)
    {
        m_addr[index++] = *iter;
    }
}

MacType::MacType(const unsigned char* MacType,int _type)
{
    if(0 == _type){
        for (unsigned int i = 0; i < sizeof(MacType); ++i) m_addr[i] = *(MacType + i);
    }else{
        int tempValue[6] = { 0 };
        sscanf((char*)MacType, "%02x:%02x:%02x:%02x:%02x:%02x",&tempValue[0], &tempValue[1], &tempValue[2], &tempValue[3], &tempValue[4], &tempValue[5]);
        for (uint8_t i = 0; i < 6; ++i) m_addr[i] = tempValue[i];
    }
}

MacType::MacType(const MacType& other)
{
    memcpy(m_addr, other.m_addr, sizeof(m_addr));
}

MacType& MacType::operator=(const MacType& other)
{
    memcpy(m_addr, other.m_addr, sizeof(m_addr));
    return *this;
}

MacType::MacType(const MacType&& other)
{
    memcpy(m_addr, other.m_addr, sizeof(m_addr));
}

MacType& MacType::operator=(const MacType&& other)
{
    memcpy(m_addr, other.m_addr, sizeof(m_addr));
    return *this;
}

bool MacType::operator==(const MacType& oth)
{
    if (m_addr[0] == oth.m_addr[0] &&
        m_addr[1] == oth.m_addr[1] &&
        m_addr[2] == oth.m_addr[2] &&
        m_addr[3] == oth.m_addr[3] &&
        m_addr[4] == oth.m_addr[4] &&
        m_addr[5] == oth.m_addr[5])
    {
        return true;
    }
    return false;
}

bool MacType::operator!=(const MacType& oth)
{
    if (*this == oth)
    {
        return false;
    }
    return true;
}

bool MacType::operator<(const MacType& other) const
{
    for (unsigned int i = 0; i < sizeof(m_addr); ++i)
    {
        if (m_addr[i] != other.m_addr[i])
        {
            return m_addr[i] < other.m_addr[i];
        }
    }
    return false;
}

void MacType::toCopy(unsigned char* MacType)
{
    memcpy(MacType, m_addr, sizeof(m_addr));
}

uint8_t& MacType::operator[](int index)
{
    return m_addr[index % sizeof(m_addr)];
}

const unsigned char* MacType::operator()() const
{
    return m_addr;
}

unsigned char* MacType::operator()()
{
    return m_addr;
}
std::string MacType::toString()
{
    char buf[20] = { 0 };
    sprintf(buf, 
        "%02x:%02x:%02x:%02x:%02x:%02x", 
        m_addr[0], m_addr[1], m_addr[2], m_addr[3], m_addr[4], m_addr[5]
    );
    return std::string(buf);
}